﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netfraction.Network.Protocol.BASE.Handler
{
    public class HandlerMSG : ProtocolMessageActionHandler
    {
        public HandlerMSG() : base()
        {
            mParameterCodes = new List<string>(new string[] {"PM", "ME"});
        }

        #region ProtocolMessageActionHandler Members

        public override string ProtocolActionCode
        {
            get { return "MSG"; }
        }

        public override bool HandleProtocolMessage(RemoteMachine.Node node, ProtocolMessage message, IEnumerable<ProtocolFeature> extenders)
        {
            if (node is RemoteMachine.Hub)
            {
                //Console.WriteLine("in MSG handler");
                if (message.Parameters.Count != 0)
                {
                    message.Parameters = DecodeParameters(message.Parameters);

                    if (message.MessageType == ProtocolMessage.MessageTypeEnum.Info) // message received from the hub directly. IMSG Registration\sarray\supdated\s(new\sCID)
                    {
                        Console.WriteLine("<{0}> {1}", node.Identity.Nickname, message.Parameters.Where(c => string.IsNullOrEmpty(c.ParameterCode)).First().ParameterValue);
                    }
                    else
                    {
                        LocalMachine.Client client = LocalMachine.Client.GetInstance();
                        var sendingPeer = client.Peers.Where(c => c.SessionId == message.SessionId);
                        if (sendingPeer.Count() > 0)
                        {
                            RemoteMachine.Peer peer = sendingPeer.First();

                            ClientInterface.ChatOptions opts = Netfraction.ClientInterface.ChatOptions.None;
                            Identity targetIdent = null;
                            Identity responseIdent = null;

                            // doesn't yet support group private chat (I don't think)
                            switch (message.MessageType)
                            {
                                case ProtocolMessage.MessageTypeEnum.Broadcast: // public chat. BMSG AAAO gsag
                                    opts = ClientInterface.ChatOptions.PublicMessage;
                                    break;
                                case ProtocolMessage.MessageTypeEnum.Echo:  // private chat, also check PM flag. EMSG AAAO AAAT spade\sgoes\salright PMAAAO
                                    var pmText = message.Parameters.Where(c => c.ParameterCode == "PM");
                                    if (pmText.Count() > 0)
                                    {
                                        if ((pmText.First() as Protocol.ProtocolParameter).ParameterValue == message.SessionId)
                                        {
                                            opts = ClientInterface.ChatOptions.PrivateMessage;
                                            responseIdent = client.Identity;
                                        }
                                        else
                                        {
                                            opts = ClientInterface.ChatOptions.GroupPrivateMessage;
                                            // god knows if this would actually work....
                                            var groupSIDSource = client.Peers.Where(c => c.SessionId == (pmText as Protocol.ProtocolParameter).ParameterValue);
                                            if (groupSIDSource.Count() > 0)
                                            {
                                                responseIdent = (groupSIDSource.First()).Identity;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        opts = ClientInterface.ChatOptions.HubMessage;  // I think it's actually from the hub bot...
                                    }
                                    var target = client.Peers.Where(c => c.SessionId == message.TargetSessionId);
                                    if (target.Count() > 0)
                                    {
                                        targetIdent = target.First().Identity;
                                    }
                                    break;
                                default:
                                    break;
                            }
                            var meText = message.Parameters.Where(c => c.ParameterCode == "ME");
                            if (meText.Count() > 0 && meText.First().ParameterValue == "1")
                            {
                                opts |= ClientInterface.ChatOptions.METext;
                            }

                            string messageText = message.Parameters.Where(c => c.ParameterCode == string.Empty).First().ParameterValue;

                            ClientInterface.Chat.ChatMessageReceived(peer.Identity, targetIdent, responseIdent, messageText, opts);

                            /*if (meText.Count() > 0 && meText.First().ParameterValue == "1")
                            {
                                Console.WriteLine("*{0} {1}", peer.Identity.Nickname, message.Parameters.Where(c => string.IsNullOrEmpty(c.ParameterCode)).First().ParameterValue);
                            }
                            else
                            {
                                Console.WriteLine("<{0}> {1}", peer.Identity.Nickname, message.Parameters.Where(c => string.IsNullOrEmpty(c.ParameterCode)).First().ParameterValue);
                            }*/
                        }
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
